package jinyilw.swing.visuals.comp;

import jinyilw.swing.icon.LazyScaleIcon;

import java.awt.Dimension;
import java.awt.Graphics;
import java.io.File;
import java.net.URL;

public class ScaleImageComp extends MouseActComp
{
	private static final long serialVersionUID = -1276675463020135887L;
	protected LazyScaleIcon lazyScaleIcon;
	private final Dimension imageSize = new Dimension();

	public ScaleImageComp()
	{
	}

	public ScaleImageComp(URL url)
	{
		setImage(url);
	}

	public ScaleImageComp(File file)
	{
		setImage(file);
	}

	public ScaleImageComp(LazyScaleIcon lrImage)
	{
		setImage(lrImage);
	}

	//	public void setImage(String name)
	//	{
	//		setImage(LazyScaleIcon.getIcon(name));
	//	}

	public void setImage(URL url)
	{
		setImage(LazyScaleIcon.getIcon(url));
	}

	public void setImage(File file)
	{
		setImage(LazyScaleIcon.getIcon(file));
	}

	public void setImage(LazyScaleIcon lrImage)
	{
		if (lazyScaleIcon == lrImage)
			return;
		lazyScaleIcon = lrImage;
		if (lrImage != null)
		{
			imageSize.width = lrImage.getIconWidth();
			imageSize.height = lrImage.getIconHeight();
		}
		if (lrImage != null && (imageSize.width != getWidth()
				|| imageSize.height != getHeight()))
			setSize(imageSize.width, imageSize.height);
		else if (isShowing())
			repaint();
	}

	@Override
	public Dimension getPreferredSize()
	{
		if (lazyScaleIcon == null)
			return super.getPreferredSize();
		return new Dimension(imageSize);
	}

	@Override
	public void paint(Graphics g)
	{
		if (lazyScaleIcon != null)
			lazyScaleIcon.paintIcon(this, g, 0, 0);
	}
}
